var Audio = Audio || function() {
      var self  = this;
      self.play = self.stop = new Function();
    };


var snowStorm = snowStorm || {};

snowStorm.className = "snow";


var trick = initTrick('trickContainer');
trick.hide();


var Lottery = (function($container) {

  var timer           = null
      , itemWidth       = 142
      , itemCount       = 0
      , curPos          = 0
      ;

  var stopAudio       = new Audio("res/ping.mp3")
      , backAudio       = new Audio("res/back.mp3")
      ;


  backAudio.loop = true;

  var  $content        = $container.children("ul")
      , $hero           = $("#lottery-hero span")
      ;
  var index = 0;

  var candicates,selected, gifts, giftSelected;

  var state = 0;

  var init = function() {

    $.ajax({
      url: $container.data("url")
      , dataType: "text"
      , success: function(data) {
        var names = data.toString().split('\n');
        candicates = names;
        show();
      }
    });

    $.ajax({
      url: "gifts.txt"
      , dataType: "text"
      , success: function(data) {
        var names = data.toString().split('\n');
        gifts = names;
      }
    });

  };

  var getCandicates = function () {
    if(state == 0){
      return candicates;
    } else if(state == 1){
      return gifts;
    }
    return [];
  }

  var show = function(){

    var showCandicates = [];
    var localIndex = index;
    var c = getCandicates();
    var max = Math.min(5, c.length);
    for(var i = 0; i < max; i ++){
      showCandicates.push(c[localIndex]);
      if(localIndex == c.length - 1){
        localIndex = 0;
      } else {
        localIndex++;
      }
    }

    var html = "";
    for (var i = 0; i < showCandicates.length; i++) {
      var name = showCandicates[i];
      html += ("<li><b>" + name + "</b></li>");
    }
    $content.html(html);
  }

  var start  = function() {
    if(Lottery.running()){
      return;
    }
    trick.hide();
    //2表示人和奖品都抽完了
    if(state == 2){
      if(!isNaN(selected)){
        candicates.splice(selected,1);
        selected = null;
      }
      if(!isNaN(giftSelected)){
        gifts.splice(giftSelected,1);
        giftSelected = null;
      }

      $hero.hide();
      state = 0;
    }


    if(candicates.length == 0){
      alert('木有人啦.');
      return;
    }

    if(gifts.length == 0){
      alert('这一轮抽光啦,去下一轮吧.');
      return;
    }

    clearInterval(timer);

    backAudio.play();
    stopAudio.pause();
    $(document.body).addClass("stop");

    index = 0;
    timer = setInterval(function() {

      if(index >= getCandicates().length - 1){
        index = 0;
      } else {
        index++;
      }

      show();

    }, 10);

  };

  var stop = function() {

    if(!running()){
      return;
    }


    clearInterval(timer);
    timer = null;

    backAudio.pause();
    stopAudio.play();
    $(document.body).removeClass("stop");

    if(state == 0){
      selected  = index;
    } else if(state == 1){
      giftSelected = index;
    }

    state++;

    updateHero();
  };

  var running = function() {
    return timer != null;
  };

  //Index: first item on the left
  var updateHero = function() {

    var selectedName = candicates[selected];
    var selectedGift = gifts[giftSelected];

    var showText = selectedName;

    if(selectedGift){
      showText += " 获得了 "+selectedGift;
      $hero.html("").show('slow');
    } else{
      $hero.html(showText).show('slow');
    }

    if(state == 2){
      trick.next(showText);
      trick.show();
    }

  };



  return {
    init: init
    , start: start
    , stop: stop
    , running: running
  };

})($("#lottery-container"));



$(document).ready(function() {
  Lottery.init();
});

$(document).keydown(function(e) {
  var key = e.keyCode;
  if (key != 32 && key != 13) return;

  Lottery.running() ? Lottery.stop() : Lottery.start();
});